//
//  ArticleCollectionView.m
//  Style
//
//  Created by YL on 15/9/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "YLArticleCollectionView.h"
#import "ArticleModel.h"
#import "YLArticleCollectionViewCell.h"
#import "AFNetWorkingW.h"
#import <AFNetworking.h>
@interface YLArticleCollectionView () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) NSMutableArray *mArr;
@property (nonatomic, retain) NSMutableDictionary *mDic;
@property (nonatomic, retain) YLArticleCollectionViewCell *articleCell;
@property (nonatomic, retain) ArticleModel *article_model;
@end
@implementation YLArticleCollectionView
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        self.alwaysBounceVertical = YES;
        
        self.scrollEnabled = NO;
        
        /* 两个协议 */
        self.delegate = self;
        self.dataSource = self;
        
        /* 背景颜色默认为黑色 */
        self.backgroundColor = [UIColor whiteColor];
        
        /* collectionView cell 必须提前注册*/
        [self registerClass:[YLArticleCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
        AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
        if (mgr.reachable == YES) {
            [self dataHandle];
        }
    }
    return self;
}


/* item数量 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.mArr count];
}

/* cell设置 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.articleCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    self.article_model = [self.mArr objectAtIndex:indexPath.item];
    self.articleCell.article_model = self.article_model;

    return self.articleCell;
}

/* 点击item,通知中心传值 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    self.article_model = [self.mArr objectAtIndex:indexPath.item];
    [center postNotificationName:@"push" object:self.article_model];
    
}

/* 数据处理 */
- (void)dataHandle
{
    
    [AFNetWorkingW netWorkWithURL:@"http://api2.pianke.me/read/columns" body:@"auth=&client=1&deviceid=BA920378-52B5-43F2-BE1A-5404120EDD7A&version=3.0.6" resultBlock:^(id result) {
        self.mDic = result;
        self.mArr = [NSMutableArray array];
        NSDictionary *dic = [self.mDic objectForKey:@"data"];
        NSArray *arr = [dic objectForKey:@"list"];
        for (NSDictionary *dic1 in arr) {
            self.article_model = [[ArticleModel alloc] init];
            [self.article_model setValuesForKeysWithDictionary:dic1];
            
            [self.mArr addObject:self.article_model];
        }
        [self reloadData];
        
    }];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
